package bma.common.langutil.ai.stack;

import bma.common.langutil.ai.callback.AICallback;
import bma.common.langutil.ai.callback.AICallbackList;
import bma.common.langutil.ai.future.AICallbackable;
import bma.common.langutil.ai.future.AIFuture;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <RESP_TYPE>
 */
public class AIStackCore<RESP_TYPE> extends AIStackAbstract<RESP_TYPE>
		implements AICallbackable<RESP_TYPE>, AIFuture<RESP_TYPE> {

	private AIStack<?> parent;
	private AICallbackList<RESP_TYPE> callbackList = new AICallbackList<RESP_TYPE>();

	public AIStackCore(AIStack<?> parent) {
		super();
		this.parent = parent;
	}

	public AIStackCore(AIStack<?> parent, AICallback<RESP_TYPE> callback) {
		super();
		this.parent = parent;
		addListener(callback);
	}

	@Override
	public AIStack<?> getParent() {
		return parent;
	}

	@Override
	public AICallback<RESP_TYPE> callback() {
		return callbackList.first();
	}

	@Override
	public boolean callback(AICallback<RESP_TYPE> callback) {
		addListener(callback);
		return false;
	}

	@Override
	public boolean addListener(AICallback<RESP_TYPE> listener) {
		return callbackList.add(listener);
	}

	@Override
	public boolean success(RESP_TYPE result) {
		callbackList.execute(result, null);
		return true;
	}

	@Override
	public boolean failure(Throwable t) {
		callbackList.execute(null, t);
		return true;
	}

}
